package com.testitems.controller.config.apiaddr;


import com.testitems.common.web.page.PageInfo;
import com.testitems.common.web.result.Result;
import com.testitems.config.security.UserInfo;
import com.testitems.controller.config.apiaddr.dto.ApiAddrAddRequest;
import com.testitems.controller.config.apiaddr.dto.ApiAddrInfo;
import com.testitems.controller.config.apiaddr.dto.ApiAddrSearchRequest;
import com.testitems.controller.config.apiaddr.dto.ApiAddrUpdateRequest;
import com.testitems.repo.entity.ApiAddrConfig;
import com.testitems.service.apiaddrconfig.IApiAddrConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 接口地址配置 前端控制器
 * </p>
 *
 * @author GFH
 * @since 2021-11-23
 */
@Slf4j
@Api(tags = "接口地址配置")
@RestController
@RequestMapping("/api/config/apiAddr")
public class ApiAddrConfigController {

    @Autowired
    private IApiAddrConfigService apiAddrConfigService;


    @PostMapping("/search")
    @ApiOperation(value = "接口地址列表查询")
    public Result search(@Validated @RequestBody ApiAddrSearchRequest request,
                         @ApiIgnore @AuthenticationPrincipal UserInfo userInfo) {
        PageInfo<ApiAddrConfig> searchInfo = apiAddrConfigService.search(userInfo, request);
        return Result.ok(searchInfo);
    }

    @GetMapping("/getApiAddrList")
    @ApiOperation(value = "接口地址下拉列表")
    public Result getApiAddrList() {
        List<ApiAddrConfig> apiAddrList = apiAddrConfigService.getApiAddrList();
        List<ApiAddrInfo> addrInfoList = apiAddrList.stream().map(item -> {
            final ApiAddrInfo apiAddrInfo = new ApiAddrInfo();
            apiAddrInfo.setName(item.getName());
            apiAddrInfo.setUrl(item.getUrl());
            return apiAddrInfo;
        }).collect(Collectors.toList());
        return Result.ok(addrInfoList);
    }

    @PostMapping
    @ApiOperation(value = "新增接口地址")
    public Result add(@Validated @RequestBody ApiAddrAddRequest request,
                      @ApiIgnore @AuthenticationPrincipal UserInfo userInfo) {
        ApiAddrConfig apiAddrConfig = apiAddrConfigService.getApiAddrConfigByName(request.getName());
        if (apiAddrConfig != null) {
            return Result.fail("当前接口已存在");
        }
        apiAddrConfigService.add(userInfo, request);
        return Result.ok();
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "变更接口地址")
    public Result update(@PathVariable Integer id, @Validated @RequestBody ApiAddrUpdateRequest request,
                         @ApiIgnore @AuthenticationPrincipal UserInfo userInfo) {
        apiAddrConfigService.updateById(id, userInfo, request);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口地址")
    public Result logicDelete(@PathVariable Integer id, @ApiIgnore @AuthenticationPrincipal UserInfo userInfo) {
        apiAddrConfigService.logicDelete(userInfo, id);
        return Result.ok();
    }

}
